package com.fish.yssb.ui.page.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.data.model.HisFileData;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.ui.page.activity.his.hisDetailYCActivity;
import com.fish.yssb.ui.page.activity.other.ShowTextActivity;
import com.fish.yssb.ui.page.activity.wave.WaveActivity;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.dialog.SetParaDialog;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class HisDetailAdapter extends RecyclerView.Adapter<HisDetailAdapter.BaseViewHolder> {
    private ArrayList<HisFileData> dataList = new ArrayList<HisFileData>();
    private Resources res;
    private Context m_context;
    public hisDetailYCActivity activity;
    public void replaceAll(ArrayList<HisFileData> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public HisDetailAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        m_context =parent.getContext();
        return new HisDetailAdapter.realtimeYCViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.his_detail_item, parent, false));
    }

    @Override
    public void onBindViewHolder(HisDetailAdapter.BaseViewHolder holder, int position) {

        holder.setData(dataList.get(position));
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(HisFileData data) {
        }
    }

    private class realtimeYCViewHolder extends HisDetailAdapter.BaseViewHolder {


        private TextView tv_name;
        private TextView tv_size;
        private TextView tv_time;
        private TextView tv_op,tv_op2;
        public realtimeYCViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_size = view.findViewById(R.id.tv_size);
            tv_time = view.findViewById(R.id.tv_time);
            tv_op = view.findViewById(R.id.tv_op);
            tv_op2 = view.findViewById(R.id.tv_op2);
        }

        @Override
        void setData(final HisFileData data) {
            if (data != null) {
                tv_name.setText(data.getFileName());
                tv_size.setText(data.getFileSize());

                tv_time.setText(data.getFileDate());
//                tv_unit.setText(data.getUnit());
                tv_op2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        activity.getFilePath(data.getFileName());

                    }
                });
                tv_op.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if("".equalsIgnoreCase(data.getFilePath()))
                        {
                            return;
                        }
                        File tFile = new File(data.getFilePath());
                        if(tFile == null || !tFile.exists())
                        {
                            return;
                        }
                        ShowTextActivity.start(m_context,data.getFileName(),data.getFilePath());
//                        Intent intent = new Intent(Intent.ACTION_VIEW);
//
//                        intent.addCategory(Intent.CATEGORY_DEFAULT);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
//                        Uri uri = Uri.fromFile(tFile);
//                        intent.setDataAndType(uri,   "text/plain");
//                        m_context.startActivity(intent);


                    }
                });

            }



        }
    }


}